package com.im.partchat.mvp.model.contact;

import com.im.partchat.adapter.tree.TreeElement;
import com.im.partchat.bean.Department;
import com.im.partchat.bean.Group;
import com.im.partchat.bean.User;
import com.im.partchat.db.dao.DeptDao;
import com.im.partchat.db.dao.UserDao;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by hhz on 2017/7/1.
 */
public class ContactModelImpl implements ContactModel {

    public Observable<List<User>> getMyOrganization(final String deptId) {
        return Observable.create(new ObservableOnSubscribe<List<User>>() {
            @Override
            public void subscribe(ObservableEmitter<List<User>> e) throws Exception {
                List<User> users = UserDao.getInstance().getUserByDeptId(deptId);
                e.onNext(users);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<List<User>> getMyFriend() {
        return Observable.create(new ObservableOnSubscribe<List<User>>() {
            @Override
            public void subscribe(ObservableEmitter<List<User>> e) throws Exception {
                List<User> users = UserDao.getInstance().getFriends();
                e.onNext(users);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<List<Group>> getMyGroup() {
        return Observable.create(new ObservableOnSubscribe<List<Group>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Group>> e) throws Exception {
                List<Group> groups = SQLite.select().from(Group.class).queryList();
                e.onNext(groups);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<List<TreeElement>> getOrganization() {
        return Observable.create(new ObservableOnSubscribe<List<TreeElement>>() {
            @Override
            public void subscribe(ObservableEmitter<List<TreeElement>> e) throws Exception {
                List<Department> deptBeans = DeptDao.getInstance().getFirstLevelDept();
                List<TreeElement> elements = new ArrayList<>();
                if (deptBeans != null && !deptBeans.isEmpty()) {
                    for (Department deptBean : deptBeans) {
                        TreeElement treeElement = new TreeElement();
                        treeElement.setDept(deptBean);
                        treeElement.setDept(true);
                        treeElement.setExpanded(true);
                        treeElement.setLevel(0);
                        elements.add(treeElement);
                        List<TreeElement> childs = new ArrayList<>();
                        List<User> personBeanList = UserDao.getInstance().getUserByDeptId(deptBean.getId());
                        if (personBeanList != null) {
                            for (User personBean : personBeanList) {
                                TreeElement personElement = new TreeElement();
                                personElement.setPerson(personBean);
                                personElement.setDept(false);
                                personElement.setExpanded(false);
                                personElement.setLevel(1);
                                personElement.setParentNode(treeElement);
                                childs.add(personElement);
                                elements.add(personElement);
                            }
                        }
                        List<Department> deptBeansList = DeptDao.getInstance().getSubDepart(deptBean.getId());
                        if (deptBeansList != null) {
                            for (Department deptBean1 : deptBeansList) {
                                TreeElement deptElement = new TreeElement();
                                deptElement.setDept(deptBean1);
                                deptElement.setDept(true);
                                deptElement.setExpanded(false);
                                deptElement.setLevel(1);
                                deptElement.setParentNode(treeElement);
                                childs.add(deptElement);
                                elements.add(deptElement);
                            }
                        }
                        treeElement.setChildren(childs);
                    }
                }
                e.onNext(elements);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<List<User>> getUsersByKey(final String key) {
        return Observable.create(new ObservableOnSubscribe<List<User>>() {
            @Override
            public void subscribe(ObservableEmitter<List<User>> e) throws Exception {
                List<User> users = UserDao.getInstance().getUserByKey(key);
                e.onNext(users);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
